<?php
namespace addon\mall\controller;
use app\common\controller\AddonAdminBase;
use tpfcore\Core;
use tpfcore\util\TimeUtil;
use think\Db;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class CashOut extends AddonAdminBase
{
    public function index(){
      $data=$search = $this->param;
      $this->assign('search',$search);
      $where=[];
      if(isset($data['status']) && is_numeric($data['status'])){
        $where['__CASH_OUT_LOG__.status']=$data['status'];
      }
      if(!empty($data['target'])){
        $where['__CASH_OUT_LOG__.target']=$data['target'];
      }
      $begin='';$end='';
      !empty($data['start_time']) ? $begin=strtotime($data['start_time']) : '';
      empty($data['end_time']) ? $end=time() : $end=strtotime($data['end_time']);
      if($begin && $end){
        $where['__CASH_OUT_LOG__.addtime'] = ['between',"$begin,$end"];
      }
      if(!empty($data['keyword'])){
        $where['cash_data']=["like","%".$data['keyword']."%"];
      }

      $list = Core::loadAddonModel("CashOutLog")->getCashOutLog([
          "where"=>$where,
          "field"=>"__CASH_OUT_LOG__.*,__USER__.nickname",
          "join"=>["join"=>"__USER__","condition"=>"__CASH_OUT_LOG__.user_id=__USER__.id","type"=>"left"],
          "order"=>"__CASH_OUT_LOG__.addtime desc",
          'paginate'=>['rows' =>10,"config"=>["query"=>$this->param]],
      ]);

    	$this->addonTemplate('index',[
        "list"=>$list
    	]);
    }
    public function del(){
    	 $this->jump(Core::loadAddonModel("CashOutLog")->delCashOutLog($this->param));
    }
    public function handle(){
      $this->jump(Core::loadAddonModel("CashOutLog")->handle($this->param)); 
    }
    //提现统计
    public function statistics(){
      $data=$search = $this->param;
      $this->assign('search',$search);

      $where=[];
      $begin=$end=$amount_where=$person_where='';

      $type = empty($data['type'])?"":$data['type'];
      switch ($type) {
        case 'today':
          $begin=TimeUtil::today()[0];
          $end=TimeUtil::today()[1];
          break;
        case 'week':
          $begin=TimeUtil::week()[0];
          $end=TimeUtil::week()[1];
          break;
        case 'month':
          $begin=TimeUtil::month()[0];
          $end=TimeUtil::month()[1];
          break;
        case 'quarter':
          $begin=TimeUtil::quarter()[0];
          $end=TimeUtil::quarter()[1];
          break;
         case 'year':
          $begin=TimeUtil::year()[0];
          $end=TimeUtil::year()[1];
          break;
        default:
          break;
      }
      if(empty($type)){
        !empty($data['start_time']) ? $begin=strtotime($data['start_time']) : '';
        empty($data['end_time']) ? $end=time() : $end=strtotime($data['end_time']);
      }

      if($begin && $end){
        $where['__CASH_OUT_LOG__.addtime'] = ['between',"$begin,$end"];
        $amount_where="AND addtime BETWEEN $begin AND $end";
        $person_where="where addtime BETWEEN $begin AND $end";
      }

      /* 计算总提现人数，上月提现、本月提现人数 */
      $month=TimeUtil::month();
      $lastMonth=TimeUtil::lastMonth();

      $sql_person = "SELECT count(*) as cash_out_person FROM (SELECT user_id FROM tpf_cash_out_log $person_where GROUP BY user_id) as t
UNION ALL
SELECT count(*) as cash_out_person1 FROM (SELECT user_id FROM tpf_cash_out_log where addtime between {$month[0]} AND {$month[1]}  GROUP BY user_id) as t1
UNION ALL
SELECT count(*) as cash_out_person2 FROM (SELECT user_id FROM tpf_cash_out_log where addtime between {$lastMonth[0]} AND {$lastMonth[1]} GROUP BY user_id) as t2
";
      $person_data = Db::query($sql_person);
      
      $cash_data_sql="SELECT 
sum(
  CASE WHEN status!=4 $amount_where THEN 1 ELSE 0 END
) as cash_out_number,
sum(
  CASE WHEN status!=4 AND addtime between {$month[0]} AND {$month[1]} THEN 1 ELSE 0 END
) as cash_out_this_month_number,
sum(
  CASE WHEN status!=4 AND addtime between {$month[0]} AND {$month[1]} THEN money ELSE 0 END
) as cash_out_this_month_money,
sum(
  CASE WHEN status!=4 AND addtime between {$lastMonth[0]} AND {$lastMonth[1]} THEN 1 ELSE 0 END
) as cash_out_last_month_number,
sum(
  CASE WHEN status!=4 AND addtime between {$lastMonth[0]} AND {$lastMonth[1]} THEN money ELSE 0 END
) as cash_out_last_month_money
FROM tpf_cash_out_log";
      
      $cash_data_amount = Db::query($cash_data_sql);

      $cash_total_sql ="SELECT lefttable.date,IFNULL(righttable.money,'0.00') as money,IFNULL(righttable.person_number,0) as person_number,IFNULL(righttable.cash_out_number,0) as cash_out_number
FROM (SELECT date_sub(CURDATE(),interval @i:=@i+1 day) as date 
from (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1) as tmp,(select @i:= -1) t) as lefttable
LEFT JOIN
(SELECT sum(money) as money,count(distinct user_id) as person_number,count(user_id) as cash_out_number,time FROM (SELECT user_id,money,FROM_UNIXTIME(addtime,'%Y-%m-%d') as time FROM tpf_cash_out_log WHERE addtime BETWEEN UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) AND UNIX_TIMESTAMP(NOW())) as t GROUP BY time) as righttable
ON lefttable.date=righttable.time";
      
      $cash_total_data = Db::query($cash_total_sql);

      $pay_data_sql="SELECT lefttable.date,IFNULL(righttable.alipay,'0.00') as alipay,IFNULL(righttable.wechat,0) as wechat,IFNULL(righttable.bankcard,0) as bankcard
FROM (SELECT date_sub(CURDATE(),interval @i:=@i+1 day) as date 
from (select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1
union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1) as tmp,(select @i:= -1) t) as lefttable
LEFT JOIN
(SELECT time,
sum(CASE WHEN target='alipay' THEN money ELSE NULL END) as alipay,
sum(CASE WHEN target='wechat' THEN money ELSE NULL END) as wechat,
sum(CASE WHEN target='bankcard' THEN money ELSE NULL END) as bankcard
FROM (SELECT target,money,FROM_UNIXTIME(addtime,'%Y-%m-%d') as time FROM tpf_cash_out_log WHERE addtime BETWEEN UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) AND UNIX_TIMESTAMP(NOW())) as t
GROUP BY time) as righttable
ON lefttable.date=righttable.time";
      
      $pay_data=Db::query($pay_data_sql);



      $this->addonTemplate('statistics',[
        "person_data"=>$person_data,
        "cash_data_amount"=>$cash_data_amount[0],
        "cash_total_data"=>$cash_total_data,
        "pay_data"=>$pay_data
      ]);
    }
}
